/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.unice.miage.oobdoo.entity;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author remi
 */
@Entity
@XmlRootElement
public class Commentaire implements Serializable, Comparable {
    
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    
    private String texte;

    private long creation;

    /**
     * Notification
     */
    private boolean vue;

    @ManyToOne
    private Photo photo;

    @ManyToOne
    private Utilisateur auteur;

    public Commentaire(){}
    
    public Commentaire(final Utilisateur auteur, final Photo photo, final String texte)
    {
        this.texte = texte;
        this.auteur = auteur;
        this.photo = photo;
        Date d = new Date();
        this.creation = d.getTime();
        this.vue = false;
    }

    public boolean isVue() {
        return vue;
    }

    public void setVue(boolean vue) {
        this.vue = vue;
    }

    public Utilisateur getAuteur() {
        return auteur;
    }

    public void setAuteur(Utilisateur auteur) {
        this.auteur = auteur;
    }

    public Photo getPhoto() {
        return photo;
    }

    public void setPhoto(Photo photo) {
        this.photo = photo;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public long getCreation() {
        return creation;
    }

    public String getCreationToString()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yy HH:mm");
        Date d = new Date(this.creation);
        return sdf.format(d);
    }

    public String getTexte() {
        return texte;
    }

    public String getTexteCompact()
    {
        String txt = texte;
        if( txt.length() > 40 )
        {
            txt = texte.copyValueOf(texte.toCharArray(), 0, 40);
            txt = txt.concat("...");
            txt = txt.replace("\n", "");
        }
        return txt;
    }

    public void setTexte(String texte) {
        this.texte = texte;
    }

    @Override
    public int compareTo(Object o) {
        Commentaire c = (Commentaire)o;

        if( this.creation > c.getCreation() )
            return 1;
        else if( this.creation < c.getCreation() )
            return -1;
        else
            return 0;

    }

    @Override
    public String toString() {
        return "Commentaire{" + "id=" + id + ", texte=" + texte + ", creation=" + creation + ", vue=" + vue + ", photo=" + photo + ", auteur=" + auteur + '}';
    }
    
    
}
